﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_Generic_ExploreModule : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public string Title { get; set; }

    public int MainItemsToDisplay {get; set;}

    public int SubItemsPerRow 
    {
        get
        {
            return listSub.GroupItemCount;
        }
        set
        {
            listSub.GroupItemCount = value;
        }
    }

    public override void DataBind()
    {
        if (DataRequired != null)
        {
            DataRequired(this, EventArgs.Empty);
        }

        if (DataSource != null && DataSource.Count > 0)
        {
            pnlMain.Visible = true;
            List<Post> mainPost = new List<Post>();
            List<Post> subPost = new List<Post>();

            mainPost.AddRange(DataSource.Take(MainItemsToDisplay));
            subPost.AddRange(DataSource.Skip(MainItemsToDisplay));

            listMain.DataSource = mainPost;
            listMain.DataBind();
            listSub.DataSource = subPost;
            listSub.DataBind();
        }
        else
        {
            pnlMain.Visible = false;
        }
        base.DataBind();
    }

    public event EventHandler DataRequired;

    protected override object SaveViewState()
    {
        ViewState["Title"] = Title;
        ViewState["MainItemsToDisplay"] = MainItemsToDisplay;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        Title = ViewState["Title"] as string;
        if (ViewState["MainItemsToDisplay"] is int)
        {
            MainItemsToDisplay = (int)ViewState["MainItemsToDisplay"];
        }
    }

    public List<Post> DataSource { get; set; }

}

